$(function() {
	//权限列表
	var dataGrid=easyExt.dataGrid();
	dataGrid.url=easyExt.url+'/sys_privilege/findAllByPage?type=O';
	dataGrid.toolbar="#toolbar";
	$('#dg').datagrid(dataGrid);
	/*
	 * 初始化菜单列表
	 */
	var treeGrid=easyExt.treeGrid();
	treeGrid.url=easyExt.url+'/sys_privilege/findAll?type=F';
	treeGrid.singleSelect=true;
	treeGrid.onClickRow=function(row){
		if($('#tg').treegrid('getLevel',row.id)==2){
			$('#dg').datagrid('load',{parentId:row.id});
		}
	}
	$('#tg').treegrid(treeGrid).treegrid('clientPaging');
	
	//删除实现
	$("#del").click(function(){
		var selRows=$('#dg').datagrid('getSelections');
		easyExt.del('id',selRows,'/sys_privilege/deleteBatch',function(){//删除成功后执行的动作，一般用于刷新datagrid
			$('#dg').datagrid('clearSelections');
			$('#dg').datagrid('reload'); 
		});
	});
	//添加实现
	$("#add").click(function(){
		easyExt.add('/sys_privilege/saveOne',function(){
			$('#addDialog').dialog('close');
			$('#dg').datagrid('reload'); 
		});
	});
	//修改实现
	$("#edit").click(function(){
		var selRows=$('#dg').datagrid('getSelections');
		easyExt.edit(selRows,'/sys_privilege/updateOne',function(){
			$('#addDialog').dialog('close');
			$('#dg').datagrid('clearSelections');
			$('#dg').datagrid('reload'); 
		});
	});
	//添加基本权限
	$("#addPermission").click(function(){
		var selRows=$('#tg').treegrid('getSelections');//返回选中行
		if(selRows.length==0){
			$.messager.alert("提示", "请选中要添加权限的二级菜单(左边树菜单)!", "info");  
			return;
		}else if(selRows.length==1){
			if($('#tg').treegrid('getLevel',selRows[0].id)==1){
				$.messager.alert("提示", "只有二级菜单才可以添加权限!", "info");  
			}else{
				$.get(easyExt.url+"/sys_privilege/addPermission",{pId:selRows[0].id},function(data){
					$('#dg').datagrid('reload'); 
				});
			}
		}
	});
	//上级菜单
	$('#parent').combotree({
		method: 'GET',
	    url: easyExt.url+'/sys_privilege/tree',
	    onSelect:function(node){
	    	$('#parentId').attr("value",node.id);
	    },
	    onBeforeSelect:function(node){
	    	if(node.attributes.pId==null){
	    		$.messager.alert("提示", "只有二级菜单才可以添加权限!", "info");
	    		return false;
	    	}
	    }
	});
});
